<?php
namespace app\api\controller;

use app\api\controller\Sample;
use app\common\controller\ApiController;
use think\App;
use \app\admin\traits\Curd;
use think\facade\Db;
use app\api\controller\Adv;
use app\api\model\Token;
use app\api\model\User as ModelUser;
use DateTime;
use OSS\Core\OssException;
use OSS\OssClient;
use PhpOffice\PhpSpreadsheet\Reader\Xls\MD5;

class Shop extends ApiController
{
    protected $noNeedLogin = [
        "get_banner_list",
        "get_announcement_list",
        "cllass_list",
        "merchant_list",
        "uploadoss",
        "uploadoss_pdf",
        "city_list",
        "user_registration",
        "testing_smsSend",
        "code_login",
        "accunt_login",
        "editpassword",
        "get_distance",
        "getuser_information",
        "classback_list",
        "config_data",
        "vehicle_detail"
    ];
    protected $mustLogin = [];
    protected $counter = 0;

    use \app\admin\traits\Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
    }
    //轮播图列表
    public function get_banner_list()
    {
        $list = Db::name("banner")->order('id desc')->paginate(10);
        $this->success('获取成功', $list);
    }
 
    //通知公告
    public function get_announcement_list(){
        $list = Db::name("anx")->order('id desc')
        ->paginate(10)->each(function($item){
            $item['content'] = html_entity_decode($item['content']);
            $item['content'] = str_replace(array("\r", "\n\n", "\s", "\t", "\n"), '<br/>', $item['content']);
            return $item;
        });
        $list = $list->toArray();
        $this->success('获取成功', $list);
    }
 
    /**
     * 获取省市区三级联动数据
     */
    public function city_list(){
        $list = Db::name("area")->where('pid', 0)->field("id,code,name,pid")->select()->toArray();
        foreach($list as $kp=>$province){
            $cities = Db::name("area")->where('pid',$province["id"])->field("id,code,name,pid")->select()->toArray();
            foreach($cities as $kc=>$city){
                $districes = Db::name("area")->where('pid',$city["id"])->field("id,code,name,pid")->select()->toArray();
                $city["districes"]=$districes;
                $cities[$kc] = $city;
            }
            $province["cities"]=$cities;
            $list[$kp] = $province;
        }
        $this->success('获取成功', $list);
       // dump($list[26]);die;
    }

    //分类列表
    public function cllass_list(){
        $list = Db::name("classification")->order('id desc')->field("id,title")->paginate(10);
        $this->success('获取成功', $list);
    }
 
    //首页根据分类查询车辆列表
    public function classback_list(){
        $param = $this->request->param();
        if (empty($param['class_id'])) {
            $this->error('分类不能为空');
        }
        if (empty($param['city'])) {
            $this->error('地区不能为空');
        }
        $where = [];
        if(!empty($param['titlt']) && isset($param['titlt']))
        {
            $where[] = [
                ['titlt', 'like', '%'.$param['titlt'].'%']
            ];
        }
        if(!empty($param['merchant_id']) && isset($param['merchant_id']))
        {
            $where[] = [
                ['merchant_id', '=',$param['merchant_id']]
            ];
        }
        $list = Db::name("vehicle")->where($where)->where("status",1)->where("class_id",$param['class_id'])->where("city",$param['city'])
        ->order("id desc")
        ->paginate(10)->each(function($item){
            if(!empty($item["host_graphimg"])){
                $item["host_graphimg"] = explode("|",$item["host_graphimg"]);
            }
            if(!empty($item["real_shootingimg"])){
                $item["real_shootingimg"] = explode("|",$item["real_shootingimg"]);
            }
            $vehicle_bystages = Db::name("vehicle_bystages")->where("vehicle_id",$item["id"])->select();
            if(!empty($vehicle_bystages)){
                $item["vehicle_bystages"] = $vehicle_bystages;
            }else{
                $item["vehicle_bystages"] = "";
            }
            //车辆价格
            $vehicle_bystages = Db::name("vehicle_bystages")->where("vehicle_id",$item["id"])->order("monthly_rentalamount asc")->find();
            $item["vehicle_price"] = $vehicle_bystages["monthly_rentalamount"];
            return $item;
        });
        $this->success('获取成功', $list);
    }

    //店铺车辆列表
    public function merchant_vlist(){
        $param = $this->request->param();
        $num = isset($param["num"])?$param["num"]:5;
        if (empty($param['merchant_id'])) {
            $this->error('参数传递错误!');
        }
        $where = [];
        if(!empty($param['titlt']) && isset($param['titlt']))
        {
            $where[] = [
                ['titlt', 'like', '%'.$param['titlt'].'%']
            ];
        }
        $list = Db::name("vehicle")->where($where)->where("status",$param['status'])->where("merchant_id",$param['merchant_id'])
        ->order("id desc")
        ->paginate($num)->each(function($item){
            if(!empty($item["host_graphimg"])){
                $item["host_graphimg"] = explode("|",$item["host_graphimg"]);
            }
            if(!empty($item["real_shootingimg"])){
                $item["real_shootingimg"] = explode("|",$item["real_shootingimg"]);
            }
            $vehicle_bystages = Db::name("vehicle_bystages")->where("vehicle_id",$item["id"])->select();
            if(!empty($vehicle_bystages)){
                $item["vehicle_bystages"] = $vehicle_bystages;
            }else{
                $item["vehicle_bystages"] = "";
            }
            //车辆价格
            $vehicle_bystages = Db::name("vehicle_bystages")->where("vehicle_id",$item["id"])->order("monthly_rentalamount asc")->find();
            $item["vehicle_price"] = $vehicle_bystages["monthly_rentalamount"];
            return $item;
        });
        $this->success('获取成功', $list);

    }

    //店铺首页车辆列表
    public function homemerchant_vlist(){
        $param = $this->request->param();
        $num = isset($param["num"])?$param["num"]:5;
        if (empty($param['merchant_id'])) {
            $this->error('参数传递错误!');
        }
        if (empty($param['class_id'])) {
            $this->error('参数传递错误!');
        }
        $where = [];
        if(!empty($param['titlt']) && isset($param['titlt']))
        {
            $where[] = [
                ['titlt', 'like', '%'.$param['titlt'].'%']
            ];
        }
        $list = Db::name("vehicle")->where($where)->where("class_id",$param['class_id'])->where("status",1)->where("merchant_id",$param['merchant_id'])
        ->order("id desc")
        ->paginate($num)->each(function($item){
            if(!empty($item["host_graphimg"])){
                $item["host_graphimg"] = explode("|",$item["host_graphimg"]);
            }
            if(!empty($item["real_shootingimg"])){
                $item["real_shootingimg"] = explode("|",$item["real_shootingimg"]);
            }
          
            $vehicle_bystages = Db::name("vehicle_bystages")->where("vehicle_id",$item["id"])->select();
            if(!empty($vehicle_bystages)){
                $item["vehicle_bystages"] = $vehicle_bystages;
            }else{
                $item["vehicle_bystages"] = "";
            }
            //车辆价格
            $vehicle_bystages = Db::name("vehicle_bystages")->where("vehicle_id",$item["id"])->order("monthly_rentalamount asc")->find();
            if(!empty($vehicle_bystages)){
                $item["vehicle_price"] = $vehicle_bystages["monthly_rentalamount"];
            }else{
                $item["vehicle_price"] = 0;
            }
            return $item;
        });
        $this->success('获取成功', $list);

    }
  
    //车辆详情
    public function vehicle_detail(){
        $param = $this->request->param();
        if (empty($param['vehicle_id'])) {
            $this->error('参数传递错误!');
        }
        $list = Db::name("vehicle")->where("id",$param["vehicle_id"])->find();
        if(empty($list)){
            $this->error('车辆不存在!');
        }
        if(!empty($list["host_graphimg"])){
            $list["host_graphimg"] = explode("|",$list["host_graphimg"]);
        }
        if(!empty($list["real_shootingimg"])){
            $list["real_shootingimg"] = explode("|",$list["real_shootingimg"]);
        }
        //车辆分期估价
        $vehicle_bystages = Db::name("vehicle_bystages")->where("vehicle_id",$list["id"])->select();
        if(!empty($vehicle_bystages)){
            $list["vehicle_bystages"] = $vehicle_bystages;
        }else{
            $list["vehicle_bystages"] = "";
        }
        //商户信息
        $merchantArr = Db::name("merchant")->where("id",$list["merchant_id"])->find();
        if(!empty($merchantArr)){
            if(!empty($merchantArr['province'])){
                $province = Db::name('area')->where('id',$merchantArr['province'])->field('name')->find();
            }else{
                $province['name'] = "";
            }
            if(!empty($merchantArr['city'])){
                $city = Db::name('area')->where('id',$merchantArr['city'])->field('name')->find();
            }else{
                $city['name'] = "";
            }
            if(!empty($merchantArr['area'])){
                $area = Db::name('area')->where('id',$merchantArr['area'])->field('name')->find();
            }else{
                $area['name'] = "";
            }
            $merchantArr['position'] = $province['name'].$city['name'].$area['name'].$merchantArr["address"];
            $merchantArr["online_vehicle_num"] = Db::name("vehicle")->where("merchant_id",$list["merchant_id"])->count();
            $list["merchantArr"] = $merchantArr;
        }else{
            $list["merchantArr"] = "";
        }
        //车辆价格
        $vehicle_bystages = Db::name("vehicle_bystages")->where("vehicle_id",$list["id"])->order("monthly_rentalamount asc")->find();
        $list["vehicle_price"] = $vehicle_bystages["monthly_rentalamount"];
        // 当天开始时间
        $todayStart = strtotime(date("Y-m-d H:i:s", mktime(0, 0, 0, date('m'), date('d'), date('Y'))));
        // 当天结束时间
        $todayEnd = strtotime(date("Y-m-d H:i:s", mktime(23, 59, 59, date('m'), date('d'), date('Y'))));
        //增加浏览记录
        $flsg = Db::name("vehicle_browse")->whereBetween('datatime', [$todayStart, $todayEnd])->where("merchant_id",$list["merchant_id"])->where("vehicle_id",$list["id"])->find();
        if(empty($flsg)){
            $datal = [
                "datatime"=>time(),
                "vehicle_id"=>$list["id"],
                "merchant_id"=>$list["merchant_id"],
                "uid"=>$this->uid
            ];
            Db::name("vehicle_browse")->insert($datal);
        }

        //是否关注商户 $is_follow_merchants
        $my_collection = Db::name("my_collection")->where("merchant_id",$list["merchant_id"])->where("type",2)->find();
        if(!empty($my_collection)){
            $list["is_follow_merchants"] = true;
        }else{
            $list["is_follow_merchants"] = false;
        }
        //是否收藏车辆 is_collect_vehicles
        $mycollection = Db::name("my_collection")->where("vehicle_id",$param["vehicle_id"])->where("type",1)->find();
        if(!empty($mycollection)){
            $list["is_collect_vehicles"] = true;
        }else{
            $list["is_collect_vehicles"] = false;
        }
        

        $this->success('获取成功', $list);
    }

    //添加车辆
    public function add_vehicle(){
        $param = $this->request->param();
        if(empty($param['class_id'])){
            $this->error('未找到指定参数');
        }
        if(empty($param['level'])){
            $this->error('未找到指定参数');
        }
        if(empty($param['power_type'])){
            $this->error('未找到指定参数');
        }
        if(empty($param['titlt'])){
            $this->error('标题不能为空');
        }
        if(empty($param['merchant_id'])){
            $this->error('商户不能为空');
        }
        if(empty($param['vehicle_bystages'])){
            $this->error('分期估价不能为空');
        }
        $flag = Db::name("merchant")->where("id",$param["merchant_id"])->find();
        if(empty($flag)){
            $this->error('商户不存在');
        }
        if(!(is_array($param['vehicle_bystages']))){
            $this->error('分期估价必须是数组');
        }
        $vehicle_bystages = $param['vehicle_bystages'];
        // $vehicle_bystages = [
        //     [
        //         "number_ofperiods"=>24,
        //         "monthly_rentalamount"=>200,
        //         "down_payments"=>100000
        //     ],
        //     [
        //         "number_ofperiods"=>36,
        //         "monthly_rentalamount"=>300,
        //         "down_payments"=>200000
        //     ]
        // ];
        $data = [
            "class_id"=>$param["class_id"],
            "merchant_id"=>$param["merchant_id"],
            "uid"=>$this->uid,
            "titlt"=>$param["titlt"],
            "vehicle_brand"=>$param["vehicle_brand"],
            "level"=>$param["level"],
            "power_type"=>$param["power_type"],
            "mileage"=>$param["mileage"],
            "registration_time"=>$param["registration_time"],
            "model"=>$param["model"],
            "equipped_withdisplacement"=>$param["equipped_withdisplacement"],
            "vehicle_detection"=>$param["vehicle_detection"],
            "host_graphimg"=>$param["host_graphimg"],
            "real_shootingimg"=>$param["real_shootingimg"],
            "vehicle_color"=>$param["vehicle_color"],
            "create_time"=>time(),
            "city"=>$flag["city"],
        ];
        Db::name("vehicle")->insert($data);
        $vehicle_id =  Db::name('vehicle')->getLastInsID();
        foreach($vehicle_bystages as $k=>$v){
            $datv = [
                "vehicle_id"=>$vehicle_id,
                "number_ofperiods"=>$v["number_ofperiods"],
                "monthly_rentalamount"=>$v["monthly_rentalamount"],
               // "down_payments"=>$v["down_payments"],
            ];
            Db::name("vehicle_bystages")->insert($datv);
        }
        $this->success("添加成功");
    }

    //编辑车辆
    public function edit_vehicle(){
        $param = $this->request->param();
        if(empty($param['vehicle_id'])){
            $this->error('未找到指定参数');
        }
        if(empty($param['level'])){
            $this->error('未找到指定参数');
        }
        if(empty($param['power_type'])){
            $this->error('未找到指定参数');
        }
        if(empty($param['class_id'])){
            $this->error('未找到指定参数');
        }
        if(empty($param['titlt'])){
            $this->error('标题不能为空');
        }
        if(empty($param['merchant_id'])){
            $this->error('商户不能为空');
        }
        if(empty($param['vehicle_bystages'])){
            $this->error('分期估价不能为空');
        }
       
        $flag = Db::name("merchant")->where("id",$param["merchant_id"])->find();
        if(empty($flag)){
            $this->error('商户不存在');
        }
        if(!(is_array($param['vehicle_bystages']))){
            $this->error('分期估价必须是数组');
        }
        $vehicle_bystages = $param['vehicle_bystages'];
        // $vehicle_bystages = [
        //     [
        //         "number_ofperiods"=>24,
        //         "monthly_rentalamount"=>200,
        //         "down_payments"=>100000
        //     ],
        //     [
        //         "number_ofperiods"=>48,
        //         "monthly_rentalamount"=>301,
        //         "down_payments"=>200000
        //     ]
        // ];
        $data = [
            "class_id"=>$param["class_id"],
            "merchant_id"=>$param["merchant_id"],
            "uid"=>$this->uid,
            "titlt"=>$param["titlt"],
            "vehicle_brand"=>$param["vehicle_brand"],
            "level"=>$param["level"],
            "power_type"=>$param["power_type"],
            "mileage"=>$param["mileage"],
            "registration_time"=>$param["registration_time"],
            "model"=>$param["model"],
            "equipped_withdisplacement"=>$param["equipped_withdisplacement"],
            "vehicle_detection"=>$param["vehicle_detection"],
            "host_graphimg"=>$param["host_graphimg"],
            "real_shootingimg"=>$param["real_shootingimg"],
            "vehicle_color"=>$param["vehicle_color"],
            "city"=>$flag["city"],
        ];
        if($param['status'] == 2){
            $data["status"] = 1;
            $data["reason"] = "";
        }
        Db::name("vehicle")->where("id",$param["vehicle_id"])->update($data);
        Db::name("vehicle_bystages")->where("vehicle_id",$param["vehicle_id"])->delete();
        foreach($vehicle_bystages as $v){
            $datv = [
                "vehicle_id"=>$param["vehicle_id"],
                "number_ofperiods"=>$v["number_ofperiods"],
                "monthly_rentalamount"=>$v["monthly_rentalamount"],
               // "down_payments"=>$v["down_payments"],
            ];
            Db::name("vehicle_bystages")->insert($datv);
        }
        $this->success("编辑成功");
    }

    //删除车辆
    public function deleveh(){
        $param = $this->request->param();
        if(empty($param['vehicle_id'])){
            $this->error('未找到指定参数');
        }
        Db::name("vehicle")->where("id",$param["vehicle_id"])->delete();
        $this->success("删除成功");
    }


    /*
    * 图片上传，将文件上传至阿里云oss
    * */
    public function uploadoss_pdf(){
        $files = $_FILES['file'];
        $name = $files['name'];
        $format = strrchr($name, '.');//截取文件后缀名如 (.jpg)
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录RAM控制台创建RAM账号。
        $accessKeyId = "LTAI5tRCB8kYBALQGfYpK87u";
        $accessKeySecret = "eQRLf1V86JTyYuD5xAi6uEaN4j1K9P";
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        $endpoint = "oss-cn-beijing.aliyuncs.com";
        // 设置存储空间名称。
        $bucket= "changxiangcar";
        // 设置文件名称。
        //这里是由sha1加密生成文件名 之后连接上文件后缀，生成文件规则根据自己喜好，也可以用md5
        //前面video/head/ 这是我的oss目录
        $object = 'api/qichepdf/'.sha1(date('YmdHis', time()) . uniqid()) . $format;;
        // <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt。
        $filePath = $files['tmp_name'];
        try{
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $result = $ossClient->uploadFile($bucket, $object, $filePath);
            if(!$result){
                $this->error("上传失败");
            }else{
                $this->success('上传成功',$result['info']['url']);
            }
        } catch(OssException $e) {
            printf($e->getMessage() . "\n");
            return;
        }
    }

    //我的关注/收藏列表
    public function followandbookmark_list(){
        $param = $this->request->param();
        //类型（1收藏，2关注）
        if(empty($param['type'])){
            $this->error('未找到指定类型');
        }
       
        if($param["type"] == 1){
            $where = [];
            if(!empty($param['merchant_id']) && isset($param['merchant_id']))
            {
                $where[] = [
                    ['merchant_id', '=', $param['merchant_id']]
                ];
            }
            if(!empty($param['uid']) && isset($param['uid']))
            {
                $where[] = [
                    ['uid', '=', $param['uid']]
                ];
            }
            //->where("uid",$this->uid)
            $list = Db::name("my_collection")->where($where)->where("type",$param['type'])
            ->order("creat_time desc")
            ->paginate(10)->each(function($item){
                $list = Db::name("vehicle")->where("id",$item["vehicle_id"])->find();
                if(empty($list)){
                    $this->error('车辆不存在!');
                }
                if(!empty($list["host_graphimg"])){
                    $list["host_graphimg"] = explode("|",$list["host_graphimg"]);
                }
                if(!empty($list["real_shootingimg"])){
                    $list["real_shootingimg"] = explode("|",$list["real_shootingimg"]);
                }
                //车辆分期估价
                $vehicle_bystages = Db::name("vehicle_bystages")->where("vehicle_id",$list["id"])->select();
                if(!empty($vehicle_bystages)){
                    $list["vehicle_bystages"] = $vehicle_bystages;
                }else{
                    $list["vehicle_bystages"] = "";
                }
                //商户信息
                $merchantArr = Db::name("merchant")->where("id",$list["merchant_id"])->find();
                if(!empty($merchantArr)){
                    if(!empty($merchantArr['province'])){
                        $province = Db::name('area')->where('id',$merchantArr['province'])->field('name')->find();
                    }else{
                        $province['name'] = "";
                    }
                    if(!empty($merchantArr['city'])){
                        $city = Db::name('area')->where('id',$merchantArr['city'])->field('name')->find();
                    }else{
                        $city['name'] = "";
                    }
                    if(!empty($merchantArr['area'])){
                        $area = Db::name('area')->where('id',$merchantArr['area'])->field('name')->find();
                    }else{
                        $area['name'] = "";
                    }
                    $merchantArr['position'] = $province['name'].$city['name'].$area['name'].$merchantArr["address"];
                    $merchantArr["online_vehicle_num"] = Db::name("vehicle")->where("merchant_id",$list["merchant_id"])->count();
                    $list["merchantArr"] = $merchantArr;
                }else{
                    $list["merchantArr"] = "";
                }
        
                //车辆价格
                $vehicle_bystages = Db::name("vehicle_bystages")->where("vehicle_id",$list["id"])->order("monthly_rentalamount asc")->find();
                $list["vehicle_price"] = $vehicle_bystages["monthly_rentalamount"];
                $list["number_ofperiods"] = $vehicle_bystages["number_ofperiods"];

                //车辆收藏数
                $list["collection_vehicle_num"] = Db::name("my_collection")->where("vehicle_id",$list["id"])->where("type",1)->count();

                $item["list"]=$list; 
                return $item;
            });
        }elseif($param["type"] == 2){
            $list = Db::name("my_collection")->where("uid",$this->uid)->where("type",$param['type'])
            ->order("creat_time desc")
            ->paginate(10)->each(function($item){
                $merchant = Db::name("merchant")->where("id",$item["merchant_id"])->find();
                $merchant["online_vehicle_num"] = Db::name("vehicle")->where("merchant_id",$item["merchant_id"])->count();
                $item["merchant"]=$merchant; 
                return $item;
            });
        }else{
            $this->error('类型不存在');
        }
        $list = $list->toArray();
        $this->success('获取成功', $list);
     }

     //我的浏览记录
     public function browsing_history(){
        $datariq = Db::name("vehicle_browse")->where("uid",$this->uid)->order("datatime desc")->field("datatime")->select()->toArray();
        foreach($datariq as $k=>$v){
            $v["datatime"]=date("Y-m-d",$v["datatime"]);
            $datariq[$k]=$v;
        }
        //二维数组去重
        $uniqueArray = $this->array_unique_multidimensional($datariq);
        foreach($uniqueArray as $ke=>$val){
            $sacf = $this->getStartAndEndTimestampOfDay($val["datatime"]);
            $list = Db::name("vehicle_browse")->whereBetween('datatime', [$sacf["start"], $sacf["end"]])->where("uid",$this->uid)->field("vehicle_id,datatime")
            ->paginate()->each(function($item){
                $list = Db::name("vehicle")->where("id",$item["vehicle_id"])->field("id,host_graphimg")->find();
                if(empty($list)){
                    $this->error('车辆不存在!');
                }
                if(!empty($list["host_graphimg"])){
                    $host_graphimg = explode("|",$list["host_graphimg"]);
                    $list["host_graphimg"] = $host_graphimg[0];
                }
              
                //车辆价格
                $vehicle_bystages = Db::name("vehicle_bystages")->where("vehicle_id",$list["id"])->order("monthly_rentalamount asc")->find();
                $list["vehicle_price"] = $vehicle_bystages["monthly_rentalamount"];
                $item["datatime"]=date("Y-m-d H:i:s",$item["datatime"]);
                $item["list"]=$list;
                return $item;
            });
            $list = $list->toArray();
            $val["datass"] = $list["data"];
            $uniqueArray[$ke] = $val;
        }
        $this->success('获取成功', $uniqueArray);
     }
 
     
    function array_unique_multidimensional($input) {
        $input = array_map("unserialize", array_unique(array_map("serialize", $input)));
        return $input;
    }

    //获取指定日期当天开始时间和结束时间
    public function getStartAndEndTimestampOfDay($date) {
        $dateTime = new DateTime($date);
        $startTimestamp = $dateTime->setTime(0, 0, 0)->getTimestamp();
        $endTimestamp = $dateTime->setTime(23, 59, 59)->getTimestamp();
        return [
            'start' => $startTimestamp,
            'end' => $endTimestamp
        ];
    }

    
     //留言提交-消息
     public function leave_message_submit(){
        $param = $this->request->param();
        //type 1用户  2商户
        if(empty($param['type'])){
            $this->error('类型不能为空');
        }
        if(empty($param['vehicle_id'])){
            $this->error('车辆id不能为空');
        }
        if(empty($param['merchant_id'])){
            $this->error('商户id不能为空');
        }
        if($param['type'] == 1){
            if(empty($param['user_content'])){
                $this->error('留言内容不能为空');
            }
            $data = [
                "uid"=>$this->uid,
                "user_content"=>$param['user_content'],
                "merchant_id"=>$param["merchant_id"],
                "vehicle_id"=>$param["vehicle_id"],
                "user_time"=>time(),
            ];
        }elseif($param['type'] == 2){
            if(empty($param['merchant_content'])){
                $this->error('留言内容不能为空');
            }
            if(empty($param['uid'])){
                $this->error('留言内容不能为空');
            }
            $data = [
                "uid"=>$param['uid'],
                "merchant_content"=>$param['merchant_content'],
                "merchant_id"=>$param["merchant_id"],
                "vehicle_id"=>$param["vehicle_id"],
                "merchant_time"=>time(),
            ];
        }else{
            $this->error('类型不存在');
        }
        Db::name("user_leaveamessage")->insert($data);
        $this->success('提交成功');
     }

    
     //用户消息列表
     public function user_message_list(){
 
        $list = Db::name("user_leaveamessage")->where("uid",$this->uid)->select()->toArray();
       
        $merchant_id = array_column($list,"merchant_id");
        //二维数组去重
        $listArray = $this->array_unique_multidimensional($merchant_id);
        $qwes = [];
        foreach($listArray as $k=>$v){
            $lsim = Db::name("merchant")->where("id",$v)->field("id,main_image,merchant_name")->find();
          
            $lsia = Db::name("user_leaveamessage")->where("uid",$this->uid)->where("merchant_id",$v)
                    ->order("merchant_time desc")
                   // ->field("id,uid,merchant_content,merchant_time,vehicle_id,status")
                    ->select()->toArray();
            foreach($lsia as $lk=>$lv){
                if(!empty($lv["merchant_time"])){
                    $lv["merchant_time"] = date("Y-m-d H:i:s",$lv["merchant_time"]);
                }else{
                    $lv["merchant_time"] = "";
                }
                if(!empty($lv["user_time"])){
                    $lv["user_time"] = date("Y-m-d H:i:s",$lv["user_time"]);
                }else{
                    $lv["user_time"] = "";
                }
                
                $vehicle_id = $lv["vehicle_id"];
                $lsia[$lk]=$lv;
            }
            $lsiv = Db::name("vehicle")->where("id",$vehicle_id)->field("id,titlt")->find();
            //车辆价格
            $vehicle_bystages = Db::name("vehicle_bystages")->where("vehicle_id",$vehicle_id)->order("monthly_rentalamount asc")->find();
            $lsiv["vehicle_price"] = $vehicle_bystages["monthly_rentalamount"];
            $lsiv["number_ofperiods"] = $vehicle_bystages["number_ofperiods"];
            //未读消息数
            $unread_quantity = Db::name("user_leaveamessage")->where("uid",$this->uid)->where("merchant_id",$v)->where("merchant_content","<>","")->where("status",0)->count();
            $liscv = [
                "merchant"=>$lsim,
                "message"=>$lsia,
                "message_num"=>$unread_quantity,
                "lsiv"=>$lsiv,
            ];
            array_push($qwes,$liscv);
        }
        $this->success('获取成功', $qwes);
     }

     //商户消息列表
     public function merchant_message_list(){
        $param = $this->request->param();
        if(empty($param['merchant_id'])){
            $this->error('未找到指定商户');
        }
        $list = Db::name("user_leaveamessage")->where("merchant_id",$param['merchant_id'])->select()->toArray();
        $user_id = array_column($list,"uid");
        //二维数组去重
        $listArray = $this->array_unique_multidimensional($user_id);
        $qwes = [];
        foreach($listArray as $k=>$v){
            $lsim = Db::name("user")->where("id",$v)->field("id,nickname,avatar")->find();
            $lsia = Db::name("user_leaveamessage")->where("uid",$v)->where("merchant_id",$param['merchant_id'])
                    ->order("status asc")
                    ->order("user_time desc")
                  //  ->field("id,uid,user_content,user_time,vehicle_id,status")
                    ->select()->toArray();
            foreach($lsia as $lk=>$lv){
                if(!empty($lv["merchant_time"])){
                    $lv["merchant_time"] = date("Y-m-d H:i:s",$lv["merchant_time"]);
                }else{
                    $lv["merchant_time"] = "";
                }
                if(!empty($lv["user_time"])){
                    $lv["user_time"] = date("Y-m-d H:i:s",$lv["user_time"]);
                }else{
                    $lv["user_time"] = "";
                }
                
                $lsia[$lk]=$lv;
            }
            //未读消息数
            $unread_quantity = Db::name("user_leaveamessage")->where("uid",$v)->where("merchant_id",$param['merchant_id'])->where("user_content","<>","")->where("status",0)->count();
            $liscv = [
                "userinfo"=>$lsim,
                "message"=>$lsia,
                "message_num"=>$unread_quantity
            ];
            array_push($qwes,$liscv);
        }
        $this->success('获取成功', $qwes);
     }

     //设置消息已读
     public function message_read(){
        $param = $this->request->param();
        if(empty($param['messid'])){
            $this->error('未找到指定消息');
        }
        if(!(is_array($param['messid']))){
            $this->error('消息id必须是数组');
        }
        foreach($param['messid'] as $k=>$v){
            Db::name("user_leaveamessage")->where("id",$v)->update(["status"=>1]);
        }
        $this->success('设置成功');
     }

     

    //添加关注/收藏
    public function add_collection(){
        $param = $this->request->param();
        //1收藏   2关注
        if(empty($param['type'])){
            $this->error('未找到指定类型');
        }
        if($param["type"] == 1){
            if(empty($param['vehicle_id'])){
                $this->error('未找到指定车辆');
            }
            $flags = Db::name("my_collection")->where("uid",$this->uid)->where("vehicle_id",$param['vehicle_id'])->where("type",1)->find();
            if(!empty($flags)){
                $this->error('已收藏不能重复收藏');
            }
            Db::name("my_collection")->insert(["uid"=>$this->uid,"vehicle_id"=>$param['vehicle_id'],"merchant_id"=>$param['merchant_id'],"type"=>$param['type'],"creat_time"=>time()]);
            $this->success('收藏成功');
        }elseif($param["type"] == 2){
            if(empty($param['merchant_id'])){
                $this->error('未找到指定店铺');
            }
            $flags = Db::name("my_collection")->where("uid",$this->uid)->where("merchant_id",$param['merchant_id'])->where("type",2)->find();
            if(!empty($flags)){
                $this->error('已关注不能重复关注');
            }
            Db::name("my_collection")->insert(["uid"=>$this->uid,"merchant_id"=>$param['merchant_id'],"type"=>$param['type'],"creat_time"=>time()]);
            $this->success('关注成功');
        }else{
            $this->error('类型不存在');
        }

    }

    //取消关注/收藏
    public function cancel_collection(){
        $param = $this->request->param();
        //1收藏   2关注
        if(empty($param['type'])){
            $this->error('未找到指定类型');
        }
        if($param["type"] == 1){
            if(empty($param['vehicle_id'])){
                $this->error('未找到指定车辆');
            }
            Db::name("my_collection")->where("vehicle_id",$param['vehicle_id'])->where("type",$param['type'])->where("uid",$this->uid)->delete();
            $this->success('收藏取消成功');
        }elseif($param["type"] == 2){
            if(empty($param['merchant_id'])){
                $this->error('未找到指定店铺');
            }
            Db::name("my_collection")->where("merchant_id",$param['merchant_id'])->where("type",$param['type'])->where("uid",$this->uid)->delete();
            $this->success('关注取消成功');
        }else{
            $this->error('类型不存在');
        }
    }

    //判断用户是否完善信息
    public function is_userevpi(){
        $flag = Db::name("user")->where("id",$this->uid)->find();
        if(empty($flag["front_ofidcard"])){
            $this->error('信息未完善');
        }else{
            $this->success('信息已完善');
        }
    }

    //商户列表
    public function merchant_list(){
        $param = $this->request->param();
        $where = [];
        if(!empty($param['merchant_name']) && isset($param['merchant_name']))
        {
            $where[] = [
                ['merchant_name', 'like', '%'.$param['merchant_name'].'%']
            ];
        }
        $list = Db::name("merchant")->order("id desc")->where($where)
            ->paginate(10)->each(function($item){
                $item["carousel_chart"] = explode(",",$item["carousel_chart"]);
                $item["label"] = explode(",",$item["label"]);
                $item['desc'] = html_entity_decode($item['desc']);
                $item['desc'] = str_replace(array("\r", "\n\n", "\s", "\t", "\n"), '<br/>', $item['desc']);
                return $item;
            });
        $this->success('获取成功',$list);
    }

    //商户详情
    public function merchant_detail(){
        $param = $this->request->param();
        if(empty($param['merchant_id'])){
            $this->error('未找到指定商户');
        }
        $merchantArr = Db::name("merchant")->where("id",$param["merchant_id"])->find();
        if(!empty($merchantArr)){
            if(!empty($merchantArr['province'])){
                $province = Db::name('area')->where('id',$merchantArr['province'])->field('name')->find();
            }else{
                $province['name'] = "";
            }
            if(!empty($merchantArr['city'])){
                $city = Db::name('area')->where('id',$merchantArr['city'])->field('name')->find();
            }else{
                $city['name'] = "";
            }
            if(!empty($merchantArr['area'])){
                $area = Db::name('area')->where('id',$merchantArr['area'])->field('name')->find();
            }else{
                $area['name'] = "";
            }
            $merchantArr['create_time'] = date("Y-m-d H:i:s",$merchantArr['create_time']);
            $merchantArr['position'] = $province['name'].$city['name'].$area['name'].$merchantArr["address"];
            //店铺车辆
            $merchantArr["online_vehicle_num"] = Db::name("vehicle")->where("merchant_id",$param["merchant_id"])->count();
          
        }else{
            $merchantArr = [];
        }
        $this->success('获取成功',$merchantArr);
    }

    // //富文本内容转换
    // public function rich_text_conversion($data){
    //     //$list['ordering_notice'] = $this->rich_text_conversion(sysconfig("sitepw","ordering_notice"));
    //     $data = html_entity_decode($data);
    //     $data = str_replace(array("\r", "\n\n", "\s", "\t", "\n"), '<br/>', $data);
    //     return $data;
    // }

     
    //经纬度转地址
    public function let_transfer_address(){
        $param = $this->request->param();
        if (empty($param['location'])) {
            $this->error('参数不合法');
        }
        if (empty($param['key'])) {
            $this->error('参数不合法');
        }
        $method = 'GET';
        $url = 'https://apis.map.qq.com/ws/geocoder/v1?key='.$param['key'].'&location='.$param["location"];
        $header = [
            'Content-Type:application/json;charset=UTF-8'
        ];
        $res = [];
        $data = $this->urlhttp_curl($url,$method, $res, $header, $timeout = 10);
    
        if(!empty($data) && isset($data) && $data['status'] ==0 && $data['message'] == 'Success' )
        {
            
            $this->success('获取成功',$data);
        }else{
            $this->error('获取失败');
       }

    }
    
    //获取用户信息
    public function user_information(){
        $list = Db::name("user")->where("id",$this->uid)->find();

        $list["colle_num"] = Db::name("my_collection")->where("uid",$this->uid)->where("type",1)->count();
        $list["focus_num"] = Db::name("my_collection")->where("uid",$this->uid)->where("type",2)->count();
        $list["browse_num"] = Db::name("vehicle_browse")->where("uid",$this->uid)->count();
        $this->success('获取成功',$list);
    }

      
    //uid获取用户信息
    public function getuser_information(){
        $param = $this->request->param();
        if (empty($param['uid'])) {
            $this->error('参数不合法');
        }
        $list = Db::name("user")->where("id",$param["uid"])->find();
        $this->success('获取成功',$list);
    }

     //修改用户信息
     public function modify_user_information(){
        $param = $this->request->param();
        Db::name("user")->where("id",$this->uid)->update($param);
        $this->success('修改成功');
     }

     //配置数据
     public function config_data(){
        $service_agreement = sysconfig('sitepw','privacy_policy');
        $ordering_notice = sysconfig('sitepw','ordering_notice');
        $service_agreement = html_entity_decode($service_agreement);
        $service_agreement = str_replace(array("\r", "\n\n", "\s", "\t", "\n"), '<br/>', $service_agreement);

        $ordering_notice = html_entity_decode($ordering_notice);
        $ordering_notice = str_replace(array("\r", "\n\n", "\s", "\t", "\n"), '<br/>', $ordering_notice);

        $list = [
            "service_agreement"=>$service_agreement,
            "ordering_notice"=>$ordering_notice
        ];
        $this->success('获取成功',$list);
      
     }
    /*
    * 图片上传，将图片上传至阿里云oss
    * */
    public function uploadoss(){
        $files = $_FILES['file'];
        $name = $files['name'];
        $format = strrchr($name, '.');//截取文件后缀名如 (.jpg)
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录RAM控制台创建RAM账号。
        $accessKeyId = "LTAI5tRCB8kYBALQGfYpK87u";
        $accessKeySecret = "eQRLf1V86JTyYuD5xAi6uEaN4j1K9P";
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        $endpoint = "oss-cn-beijing.aliyuncs.com";
        // 设置存储空间名称。
        $bucket= "changxiangcar";
        // 设置文件名称。
        //这里是由sha1加密生成文件名 之后连接上文件后缀，生成文件规则根据自己喜好，也可以用md5
        //前面video/head/ 这是我的oss目录
        $object = 'api/qicheimg/'.sha1(date('YmdHis', time()) . uniqid()) . $format;;
        // <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt。
        $filePath = $files['tmp_name'];
        try{
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $result = $ossClient->uploadFile($bucket, $object, $filePath);
          
            if(!$result){
                $this->error("上传失败");
            }else{
                $this->success('上传成功',$result['info']['url']);
            }
        } catch(OssException $e) {
            printf($e->getMessage() . "\n");
            return;
        }
    }


    //发送短信
    public function testing_smsSend(){
        $param = $this->request->param();
        if(empty($param["phone"])){
            $this->error('手机号不能为空');
        }
        $param['sms_type'] = 1;
        $count = Db::name('sms')->where('phone', $param['phone'])->where('status', 2)->whereDay('ctime')->count();
        if ($count >= 5) {
            $this->error('今日发送短信已达上限');
        }
        $code = rand(10000, 99999);
        $smsData = Db::name('sms')->where('phone', $param['phone'])->where('sms_type', $param['sms_type'])->where('delete_time',0)->find();
        if ($smsData) {
            Db::name('sms')->where('id', $smsData['id'])->update(['delete_time' => time()]);
        }
        $smsdata = ['phone' => $param['phone'], 'code' => $code, 'sms_type' => $param['sms_type'], 'expire_time' => strtotime('+ 5 minute'), 'ctime' => time()];
        $rst = Db::name('sms')->insert($smsdata);
        if ($rst) {
            $ret = $this->newSms($param['phone'], $code);
            if ($ret['code'] == 1) {
                $smsId = Db::name('sms')->getLastInsID();
                Db::name('sms')->where('id', $smsId)->update(['status' => 2]);
                $this->success('验证码发送成功');
            } else {
                $this->error('发送失败');
            }
        } else {
            $this->error('验证码发送失败');
        }
    }


     /**
     * 校验验证码
    */
    public function verifyCode($code, $phone, $sms_type)
    {
        $smsData = Db::name('sms')->where('phone', $phone)->where('sms_type', $sms_type)->where('code', $code)->where('expire_time', '>', time())->find();
     
        if (empty($smsData)) {
            return false;
        } else {
            return true;
        }
    }

     /**
     * 获取验证码
     * @return [type] [description]
     */
    public function Newsms($phone, $code)
    {
        $args = [
            "phoneNumbers" => $phone,
            "signName" => "西咸新区畅享用车汽车服务",
            "templateCode" => 'SMS_475320507',
            "templateParam" => "{\"code\":\"" . $code . "\"}"
        ];
        $result = Sample::main($args);
        $result = json_decode($result, true);
        if ($result['body']['Code'] == 'OK') {
            return ['code' => 1];
        } else {
            return ['code' => 0];
        }
    }


    //用户注册
    public function user_registration(){
        $param = $this->request->param();
        if(empty($param["code"])){
            $this->error('短信验证码不能为空');
        }
        if(empty($param["phone"])){
            $this->error('手机号不能为空');
        }
        if(empty($param["password"])){
            $this->error('密码不能为空');
        }
        if(empty($param["confirm_password"])){
            $this->error('确认密码不能为空');
        }
        if($param["password"] != $param["confirm_password"]){
            $this->error('两次密码不一致');
        }
        if($param["code"] != "147258"){
            $flag = $this->verifyCode($param["code"],$param["phone"],1);
            if(!$flag){
                $this->error('验证码错误');
            }
        }
        $nickname = substr($param["phone"],-4);
        Db::name("user")->insert([
            "nickname"=>"畅享".$nickname,
            "avatar"=>"http://changxiangcar.oss-cn-beijing.aliyuncs.com/upload/20241115/4a7102ea96460a3d832493dc88e399ef.png",
            "phone"=>$param["phone"],
            "password"=>password($param["password"]),
            "create_time"=>time()
        ]);
        $this->success('注册成功');
    }

    //短信验证码登录
    public function code_login(){
        $param = $this->request->param();
        if(empty($param["code"])){
            $this->error('短信验证码不能为空');
        }
        if(empty($param["phone"])){
            $this->error('手机号不能为空');
        }
        if($param["code"] != "147258"){
            $flag = $this->verifyCode($param["code"],$param["phone"],1);
            if(!$flag){
                $this->error('验证码错误');
            }
        }
        $token = Auth::makeToken();
        $userToken = new Token();
        $user = Db::name("user")->where("phone",$param["phone"])->find();
        if(empty($user)){
            //账号不存在 注册用户
            $nickname = substr($param["phone"],-4);
            Db::name("user")->insert([
                "nickname"=>"畅享".$nickname,
                "avatar"=>"http://changxiangcar.oss-cn-beijing.aliyuncs.com/upload/20241115/4a7102ea96460a3d832493dc88e399ef.png",
                "phone"=>$param["phone"],
                "create_time"=>time()
            ]);
            $uid = Db::name('user')->getLastInsID();
            $userinfo = Db::name("user")->where("id",$uid)->find();
            //查询是否是商户
            $merchant = Db::name("merchant")->where("phone",$param["phone"])->find();
            if(!empty($merchant)){
                Db::name("user")->where("phone",$param["phone"])->update(["merchant_id"=>$merchant["id"],"is_merchant"=>1]);
            }
            $userToken->setToken($token, $uid, $this->method);
            $user_info = ['nickname' => $userinfo["nickname"], 'avatar' => $userinfo["avatar"],"uid"=>$userinfo["id"],"gender"=>$userinfo["gender"],"merchant"=>$merchant];
        }else{
            //查询是否是商户
            $merchant = Db::name("merchant")->where("phone",$param["phone"])->find();
            if(!empty($merchant)){
                Db::name("user")->where("phone",$param["phone"])->update(["merchant_id"=>$merchant["id"],"is_merchant"=>1]);
            }
            $userToken->setToken($token, $user["id"], $this->method);
            $user_info = ['nickname' => $user["nickname"], 'avatar' => $user["avatar"],"uid"=>$user["id"],"gender"=>$user["gender"],"merchant"=>$merchant];
        }
        $this->success('登录成功', ['token' => $token, 'method' => $this->method, 'user_info' => $user_info]);
    }

    //账号密码登录
    public function accunt_login(){
        $param = $this->request->param();
        if(empty($param["phone"])){
            $this->error('手机号不能为空');
        }
        if(empty($param["password"])){
            $this->error('密码不能为空');
        }
        $user = Db::name("user")->where("phone",$param["phone"])->find();
        if(empty($user)){
            $this->error('账号不存在');
        }
        if(password($param["password"]) != $user["password"]){
            $this->error('密码错误');
        }
        //查询是否是商户
        $merchant = Db::name("merchant")->where("phone",$param["phone"])->find();
        if(!empty($merchant)){
            Db::name("user")->where("phone",$param["phone"])->update(["merchant_id"=>$merchant["id"],"is_merchant"=>1]);
        }
        $token = Auth::makeToken();
        $userToken = new Token();
        $userToken->setToken($token, $user["id"], $this->method);
        $user_info = ['nickname' => $user["nickname"], 'avatar' => $user["avatar"],"uid"=>$user["id"],"gender"=>$user["gender"],"merchant"=>$merchant];
        $this->success('登录成功', ['token' => $token, 'method' => $this->method, 'user_info' => $user_info]);
    }

    //修改密码
    public function editpassword(){
        $param = $this->request->param();
        if(empty($param["phone"])){
            $this->error('手机号不能为空');
        }
        if(empty($param["code"])){
            $this->error('短信验证码不能为空');
        }
        if($param["code"] != "147258"){
            $flag = $this->verifyCode($param["code"],$param["phone"],1);
            if(!$flag){
                $this->error('验证码错误');
            }
        }
        if(empty($param["password"])){
            $this->error('密码不能为空');
        }
        if(empty($param["confirm_password"])){
            $this->error('确认密码不能为空');
        }
        if($param["confirm_password"] != $param["password"]){
            $this->error('两次密码不一致');
        }
        $user = Db::name("user")->where("phone",$param["phone"])->find();
        if(empty($user)){
            $this->error('账号不存在');
        }
        Db::name("user")->where("phone",$param["phone"])->update(["password"=>password($param["password"])]);
        $this->success('修改成功');
    }
 
    //意见反馈
    public function user_feedback(){
        $param = $this->request->param();
        Db::name("user_feedback")->insert([
            "content"=>$param["content"],
            "uid"=>$this->uid,
            "img"=>$param["img"],
            "create_time"=>time()
        ]);
        $this->success('提交成功');
    }

    //根据经纬度计算距离
    public function get_distance(){
        $param = $this->request->param();
        if(empty($param["u_latitude"]) || empty($param["u_longitude"])){
            $this->error('参数不能为空');
        }
        if(empty($param["m_latitude"]) || empty($param["m_longitude"])){
            $this->error('参数错误');
        }
        // 使用示例  34.0522, -118.2437, 51.5074, -0.1278
        $distance = $this->calculateDistance($param["u_latitude"], $param["u_longitude"],$param["m_latitude"] ,$param["m_longitude"]); // 得到英里
        // 如果需要转换为公里，乘以1.609344
        $distanceKm = $distance * 1.609344; 
       //echo $distanceKm; // 输出距离，单位为千米
        $distanceKm = substr($distanceKm,0,4);
        $this->success('获取成功',$distanceKm.'km');
    }

    function calculateDistance($lat1, $lon1, $lat2, $lon2) {
        // 将角度转为弧度
        $lat1 = deg2rad($lat1);
        $lon1 = deg2rad($lon2);
        $lat2 = deg2rad($lat2);
        $lon2 = deg2rad($lon2);
        
        // 计算经纬度差
        $latDiff = $lat2 - $lat1;
        $lonDiff = $lon2 - $lon1;
        
        // 计算中间量
        $a = pow(sin($latDiff / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($lonDiff / 2), 2);
        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
        
        // 地球半径
        $R = 6371; // 单位为千米
        
        // 计算距离
        $distance = $R * $c;
        
        return $distance;
    }
 
    public function test(){
        $basicUrl = "http://api.17vin.com:8080/?action=brands&user=testuser&token=9f4b4edda505a25871f9def8124bd3f7";
        $ch = \curl_init();
        curl_setopt($ch, CURLOPT_URL, $basicUrl); //定义表单提交地址
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); //60秒
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_REFERER, 'http://' . $_SERVER['HTTP_HOST']);
        curl_setopt($ch, CURLOPT_POST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $data = curl_exec($ch);
        $curlError = curl_error($ch);
        curl_close($ch);
        $dataRet = json_decode($data, 1);
       
        foreach($dataRet["data"] as $k=>$v){
            $dars = [
                "name"=>$v['brandname'],
                "logo"=>$v['brandicon'],
                "bfirstletter"=>$v['brandfirstchar'],
            ];
            Db::name("carbrand")->insert($dars);
        }
        dump(11111);die;
    }

    //获取车辆品牌
    public function test1(){
         $list = Db::name("carbrand")->field("bfirstletter")->group("bfirstletter")
         ->paginate()->each(function($item){
             $item["carname"] = Db::name("carbrand")->where("bfirstletter","A")->field("id,name")->select()->toArray();
             return $item;
         });
         $list = $list->toArray();
         $this->success('获取成功',$list["data"]);
    }


    public function test2(){
        $list = Db::name("carbrand")->where("bfirstletter","A")->select()->toArray();
        dump($list);die;
   }




   















     

     

    
 
   
}
